<?php


// mail module

function do_mail($game_id, $to_email, $from_name, $reason){
	global $CFG_HOMEPAGE, $CFG_SITENAME, $CFG_MAILADDRESS;

	$subject = 'iShudan : ';
	$header  = "From: {$CFG_MAILADDRESS}\r\n\r\n";

	switch($reason)	{
		case 'invite' :
			$subject .= 'Invitation';
			$message = "You have been invited by {$from_name} to play a game of iShudan.\n\n"
							 . "Please go to {$CFG_HOMEPAGE} to accept or deny this invitation.";
			break;

		case 'yourturn' :
			$subject .= "Your Turn against {$from_name}";
			$message = "It is now your turn in game #{$game_id} against {$from_name}\n\n"
							 . "Please go to {$CFG_HOMEPAGE} to make your move.";
			break;

		case 'joinedopen' :
			$subject .= 'Open Game Filled';
			$message = "{$from_name} has joined your open game #{$game_id}.\n\n"
							 . "It may be your turn.  Please go to {$CFG_HOMEPAGE} to check.";
			break;

		case 'resigned' :
			$subject = "Game Over against {$from_name}";
			$message = "{$from_name} has resigned in game #{$game_id}.\n\n"
							 . "Go to {$CFG_HOMEPAGE}, and start a new game.";
			break;

		case 'passed' :
			$subject = "Game Over against {$from_name}";
			$message = "{$from_name} has passed in game #{$game_id}.\n\n"
							 . "Go to {$CFG_HOMEPAGE}, and count your stones to determine a winner.";
			break;

		case 'wakeup' :
			$subject = "Wake Up from {$from_name}";
			$message = "{$from_name} has sent you a wake up call.\n"
							 . "It is your turn in game #{$game_id}\n\n"
							 . "Please go to {$CFG_HOMEPAGE} to make your move.";
			break;
	}

	// add the disclaimer
	$message .= "\n\n--------------------------------------------------------\n"
	         .  "This email has been automatically generated\n"
	         .  "and should not be replied to.\n"
	         .  "~{$CFG_SITENAME}";

	// send the mail (make sure of valid email address first)
	if (preg_match('/\\b[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b/i', $to_email)){
		@mail($to_email, $subject, $message, $header);
	}
}


?>